﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class manager_user : System.Web.UI.Page
{
    private string TABLE_MANAGER = "Manager";
    private SQLQuery sqlQuery = null;
    private Connection myConnection = null;
    private string txtUser, txtPass, txtCPass, txtName, txtEmail, txtPhone, txtPermission;

    private string path = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["username"] == null)
        {
            Response.Redirect("../index.htm");
        }
        myConnection = new Connection();
        if (!myConnection.isConnectionDatabase())
        {
            Response.Redirect("../disconnect.htm");
        }

        path = Server.MapPath(".") + "\\..//image//avatar//user//";
        sqlQuery = new SQLQuery();
        if (!IsPostBack)
        {
            fillDataToGridView();
            showGridView();
        }        
    }

    private void showGridView()
    {
        layout_gridview.Visible = true;
        layout_infor.Visible = false;
    }

    private void showLayoutInfor()
    {
        layout_gridview.Visible = false;
        layout_infor.Visible = true;
    }

    private void fillDataToGridView()
    {        
        sqlQuery.loadDataGridView(mGridView, LocalVariables.SQLQUERY_SELECT_TABLE_MANAGER);        
    }

    protected void btn_reset_Click(object sender, EventArgs e)
    {
        clearText();
    }

    private void clearText()
    {
        lbl_msg.Text = "";
        lbl_not_data.Text = "";
        txt_user.Text = "";
        txt_password.Text = "";
        txt_cpassword.Text = "";
        txt_name.Text = "";
        txt_email.Text = "";
        txt_phone.Text = "";
        ddl_permission.SelectedIndex = 0;
    }


    protected void btn_edit_Click(object sender, EventArgs e)
    {
        txtUser = txt_user.Text.ToString().Trim();
        txtPass = txt_password.Text.ToString().Trim();
        if (txtPass.Length > 0)
        {
            if (LocalVariables.isNullorEmpty(txtPass))
            {
                lbl_msg.Text = LocalVariables.PASSWORD_FAILURE;
                return;
            }
            if (txtPass.Length < 6)
            {
                lbl_msg.Text = LocalVariables.PASSWORD_CHARACTERS;
                return;
            }
            txtCPass = txt_cpassword.Text.ToString().Trim();
            if (txtPass != txtCPass)
            {
                lbl_msg.Text = LocalVariables.CPASSWORD_FAILURE;
                return;
            }
        }        
        txtName = txt_name.Text.ToString().Trim();
        txtEmail = txt_email.Text.ToString().Trim();
        txtPhone = txt_phone.Text.ToString().Trim();
        if (txtPhone.Equals(""))
        {
            txtPhone = "0";
        }
        txtPermission = ddl_permission.Text.ToString().Trim();

        if (file_upload.HasFile)
        {
            LocalVariables.SAVE_VALUE_AVATAR = LocalVariables.getFileName(txtUser, file_upload.FileName);
            file_upload.SaveAs(path + LocalVariables.SAVE_VALUE_AVATAR);
        }
        if (LocalVariables.SAVE_VALUE_EMAIL != txtEmail)
        {
            if (sqlQuery.selectEmailIsExits(txtEmail, TABLE_MANAGER))
            {
                lbl_msg.Text = LocalVariables.EMAIL_BEEN_USED;
                return;
            }
        }
        if (sqlQuery.updateTableManager(txtUser, txtPass, txtName, txtEmail, txtPhone, txtPermission, LocalVariables.SAVE_VALUE_AVATAR))
        {
            showGridView();
            fillDataToGridView();
            lbl_not_data.Text = LocalVariables.EDIT_PROFILE_USER;
        }
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        txtUser = txt_user.Text.ToString().Trim();
        txtPass = txt_password.Text.ToString().Trim();
        if (txtPass.Length < 6)
        {
            lbl_msg.Text = LocalVariables.PASSWORD_CHARACTERS;
            return;
        }
        txtCPass = txt_cpassword.Text.ToString().Trim();
        txtName = txt_name.Text.ToString().Trim();
        txtEmail = txt_email.Text.ToString().Trim();
        txtPhone = txt_phone.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(txtPhone))
        {
            txtPhone = "0";
        }
        txtPermission = ddl_permission.Text.ToString().Trim();
        
        if (sqlQuery.selectUserIsExits(txtUser, TABLE_MANAGER))
        {
            lbl_msg.Text = LocalVariables.USERS_BEEN_USED;
        }
        else
        {
            if (sqlQuery.selectEmailIsExits(txtEmail, TABLE_MANAGER))
            {
                lbl_msg.Text = LocalVariables.EMAIL_BEEN_USED;
                return;
            }
            LocalVariables.SAVE_VALUE_AVATAR = "";
            if (file_upload.HasFile)
            {
                LocalVariables.SAVE_VALUE_AVATAR = LocalVariables.getFileName(txtUser, file_upload.FileName);
                file_upload.SaveAs(path + LocalVariables.SAVE_VALUE_AVATAR);
            }
            if (sqlQuery.insertTableManager(txtUser, txtPass, txtName, txtEmail, txtPhone, txtPermission, LocalVariables.SAVE_VALUE_AVATAR))
            {
                lbl_not_data.Text = LocalVariables.ADD_USER_SUCCESS;
                showGridView();
                fillDataToGridView();
            }
        }
    }
        
    protected void mGridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        showLayoutInfor();

        txt_user.Enabled = false;
        btn_save.Visible = false;
        btn_reset.Visible = false;
        btn_edit.Visible = true;
        btn_back.Visible = true;

        lbl_msg.Text = "";
        lbl_not_data.Text = "";
        ddl_permission.Text = mGridView.SelectedRow.Cells[1].Text;
        txt_user.Text = mGridView.SelectedRow.Cells[2].Text;
        txt_name.Text = mGridView.SelectedRow.Cells[4].Text;
        txt_email.Text = mGridView.SelectedRow.Cells[5].Text;
        LocalVariables.SAVE_VALUE_EMAIL = mGridView.SelectedRow.Cells[5].Text;        
        string phone = mGridView.SelectedRow.Cells[6].Text.ToString().Trim();
        if(phone != "0")
        {
            txt_phone.Text = phone;
        }
        LocalVariables.SAVE_VALUE_AVATAR = mGridView.SelectedRow.Cells[7].Text.ToString().Trim();
        if (!LocalVariables.isNullorEmpty(LocalVariables.SAVE_VALUE_AVATAR))
        {
            img_avatar.ImageUrl = "../image/avatar/user/" + LocalVariables.SAVE_VALUE_AVATAR;
        }
        else
        {
            img_avatar.ImageUrl = "../image/manager/no_avatar.jpg";
        }
    }

    protected void mGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        mGridView.PageIndex = e.NewPageIndex;
        fillDataToGridView();
    }


    protected void btn_back_Click(object sender, EventArgs e)
    {
        txt_user.Enabled = true;
        showGridView();      
        clearText();
    }

    protected void btn_add_Click(object sender, EventArgs e)
    {
        showLayoutInfor();
        clearText();
        btn_edit.Visible = false;
        btn_save.Visible = true;
        btn_reset.Visible = true;
        btn_back.Visible = true;
        txt_user.Enabled = true;
        img_avatar.ImageUrl = "../image/manager/no_avatar.jpg";
    }

    protected void btn_search_Click(object sender, EventArgs e)
    {
        string value = txt_search.Text.ToString().Trim();
        if (LocalVariables.isNullorEmpty(value))
        {
            lbl_not_data.Text = LocalVariables.KEYWORD_FAILURE;
            return;
        }
        lbl_not_data.Text = "";
        if (sqlQuery.searchInforTableMamager(mGridView, value))
        {
            lbl_not_data.Text = LocalVariables.SEARCH_NOT_RESULTS;
        }
    }

    protected void btn_refresh_Click(object sender, EventArgs e)
    {
        lbl_not_data.Text = "";
        txt_search.Text = "";
        fillDataToGridView();
    }

    protected void btn_del_Click(object sender, EventArgs e)
    {
        try
        {
            HtmlInputCheckBox chk = new HtmlInputCheckBox();
            for (int i = 0; i < mGridView.Rows.Count; i++)
            {
                chk = (HtmlInputCheckBox)mGridView.Rows[i].FindControl("chk");
                if (chk.Checked == true)
                {
                    sqlQuery.deleteTable(TABLE_MANAGER, "[user]", chk.Value.ToString());
                }
            }
            fillDataToGridView();
        }
        catch {}
    }
}
